/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author JArik
 */
public class Catalog {

    public static int addDrug(String what, String values) throws SQLException
    {
        return addToCatalog("drugs", what, values);
    }

    public static int addItem(String what, String values) throws SQLException {
        return addToCatalog("drugs", what, values);
    }

    public static int addDistr(String what, String values) throws SQLException
    {
        return addToCatalog("distributors", what, values);
    }

    public static int addOtherDistr(String what, String values) throws SQLException {
        return addToCatalog("distributors_other", what, values);
    }

    public static int addOperation(String what, String values) throws SQLException
    {
        return addToCatalog("operations", what, values);
    }


    public static void deleteBreed(int id) throws SQLException {
        deleteFromCatalog("breeds",id);
    }

    public static void deleteItem(int id)throws SQLException
    {
        deleteFromCatalog("drugs",id);
    }

    public static void deleteOperation(int id)throws SQLException
    {
        deleteFromCatalog("operations",id);
    }
    public static void deleteDiagnose(int id)throws SQLException
    {
        deleteFromCatalog("diagnoses",id);
    }
    public static void deleteSklad(int id)throws SQLException
    {
        deleteFromCatalog("sklads",id);
    }


    public static void changeItem(int id, String type, String name,
                               String descr, String izmType) throws SQLException
    {
        if(id <=0)
            throw new SQLException("Неправельный номер.");
        if(name == null || name.isEmpty())
            throw new SQLException("Нет имени.");

        String toUpdate ="name='"+name+"' ";
        if(type != null && !type.isEmpty())
          toUpdate +=", type='"+type+"' ";
        if(descr != null && !descr.isEmpty())
          toUpdate +=", description='"+descr+"' ";
        if(izmType != null && !izmType.isEmpty())
          toUpdate +=", izm='"+izmType+"' ";

        System.out.println(toUpdate);
        Database db = new Database();
        db.updateDBInfo("drugs", toUpdate, " id="+id);
        db.closeStatement();
        
    }

    public static void changeDiagnose(int id, String name, String rusName)
                                                            throws SQLException
    {
        if(id <=0)
            throw new SQLException("Неправельный номер.");
        if((name == null && rusName == null) ||
                                          (name.isEmpty() && rusName.isEmpty()))
            throw new SQLException("Нет имени.");

        String toUpdate ="name='"+name+"' ";
        if(rusName != null && !rusName.isEmpty())
          toUpdate +=", rusName='"+rusName+"' ";

        System.out.println(toUpdate);
        Database db = new Database();
        db.updateDBInfo("diagnoses", toUpdate, " id="+id);
        db.closeStatement();
    }

    public static void changeOperation(int id, String name)
                                                            throws SQLException
    {
        if(id <=0)
            throw new SQLException("Неправельный номер.");
        if(name == null || name.isEmpty())
            throw new SQLException("Нет имени.");

        String toUpdate ="name='"+name+"' ";

        Database db = new Database();
        db.updateDBInfo("operations", toUpdate, " id="+id);
        db.closeStatement();
    }
    public static void changeBreed(int id, String name) throws SQLException
    {
        if(id <=0)
            throw new SQLException("Неправельный номер.");
        if(name == null || name.isEmpty())
            throw new SQLException("Нет имени.");

        String toUpdate ="name='"+name+"' ";
        Database db = new Database();
        db.updateDBInfo("breeds", toUpdate, " id="+id);
        db.closeStatement();
    }

    private static int addToCatalog(String table, String what, String values) throws SQLException
    {
        Database db = new Database();
        ResultSet rs = db.insertIgnoreDBInfo(table, what, values);
        ArrayList<Integer> keys = new ArrayList<Integer>();
        int res=0;
        if(rs.next())
        {
            res=rs.getInt(1);
        }
        db.closeStatement();
        db = null;
        if(res==0)
            throw new SQLException("Невозможно добавить в каталог.");
        return res;
    }
    private static void deleteFromCatalog(String table, int id) throws SQLException
    {
        Database db = new Database();
        db.updateDBInfo(table, "deleted=1", " id="+id);
        db.closeStatement();
    }

}